package com.LeeCode;

import java.util.HashSet;
import java.util.Set;

/**
 * 删除子数组的最大得分
 */

public class Code1695 {
    public static void main(String[] args) {
        int[] nums = {5, 2, 1, 2, 5, 2, 1, 2, 5};
        System.out.println(new Code1695().maximumUniqueSubarray(nums));
    }

    public int maximumUniqueSubarray(int[] nums) {
        int ans = 0, sum = 0, n = nums.length;
        Set<Integer> set = new HashSet<>();

        int rk = -1;

        for (int i = 0; i < nums.length; i++) {
            if (i != 0) {
                set.remove(nums[i - 1]);
                sum -= nums[i - 1];
            }

            while (rk + 1 < n && !set.contains(nums[rk + 1])) {
                set.add(nums[rk + 1]);
                sum += nums[rk + 1];
                rk++;
            }

            ans = Math.max(ans, sum);
        }
        return ans;
    }
}
